package com.gc.common.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * 日期操作的工具类 (在struts2中可以不使用，使用struts.xml中的统一配置)
 * @author zh-heng
 */
public class DateUtil {
	public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");// 默认的DateFormat，格式为"yyyy-MM-dd"
	/**
	 * 使用默认的日期格式转换器，格式必须为"yyyy-MM-dd"，如果想使用其他的格式，请使用toDate(String dateStr,String
	 * dateFormatPattern)
	 * 
	 * @param dateStr 日期字符串
	 * @return 返回转换后的日期
	 */
	public static Date toDate(String dateStr) {
		return toDate(dateStr, dateFormat);
	}

	/**
	 * 按照用户指定的日期格式（dateFormatPattern）转换字符串dateStr为日期
	 * 
	 * @param dateStr 日期字符串
	 * @param dateFormatPattern
	 *            用户指定的日期格式
	 * @return 返回转换后的日期
	 */
	public static Date toDate(String dateStr, String dateFormatPattern){
		DateFormat dateFormat = getDateFormat(dateFormatPattern);
		return toDate(dateStr, dateFormat);
	}

	/**
	 * 按照指定的日期格式将日期转换成字符串
	 * 
	 * @param date 要进行格式化的日期
	 * @param dateFormatPattern
	 * @return 返回日期格式化后的字符串
	 */
	public static String format(Date date, String dateFormatPattern) {
		DateFormat dateFormat = getDateFormat(dateFormatPattern);
		return format(date, dateFormat);
	}

	/**
	 * 按照默认的日期格式（yyyy-MM-dd）将日期转换成字符串
	 * 
	 * @param date date 要进行格式化的日期
	 * @return 返回日期格式化后的字符串
	 */
	public static String format(Date date) {
		return format(date, dateFormat);
	}

	public static Date toDate(String dateStr, DateFormat dateFormat) {
		if (dateStr == null || dateStr.trim().length() == 0)
			throw new RuntimeException("转换为日期的字符串不能为空！");
		if (dateFormat == null)
			dateFormat = DateUtil.dateFormat;

		try {
			return dateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException("日期格式错误，转换出错！");
		}
	}

	private static String format(Date date, DateFormat dateFormat) {
		if (date == null)
			return "";
		if (dateFormat == null)
			dateFormat = DateUtil.dateFormat;
		return dateFormat.format(date);
	}

	private static DateFormat getDateFormat(String dateFormatPattern) {
		DateFormat dateFormat = null;
		if (dateFormatPattern == null || dateFormatPattern.trim().length() == 0)
			dateFormat = DateUtil.dateFormat;
		else
			dateFormat = new SimpleDateFormat(dateFormatPattern);
		return dateFormat;
	}

	/**
	 * method 将字符串类型的日期转换为一个timestamp（时间戳记java.sql.Timestamp） dateString
	 * 需要转换为timestamp的字符串 dataTime timestamp
	 * 
	 * @throws ParseException
	 */
	public static final java.sql.Timestamp string2Time(String dateString,
			String format) throws java.text.ParseException {
		if ("".equals(format) || format == null) {
			format = "yyyy-MM-dd kk:mm:ss.SSS";
		}
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat(format, Locale.ENGLISH);// 设定格式:"yyyy-MM-dd kk.mm.ss.SSS"
		// dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss",
		// Locale.ENGLISH);
		dateFormat.setLenient(false);
		java.util.Date timeDate = dateFormat.parse(dateString);// util类型
		java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());// Timestamp类型,timeDate.getTime()返回一个long型
		return dateTime;
	}
	
	/**
	 * 一个timestamp（时间戳记java.sql.Timestamp）装换为字符串类型的日期
	 * @return
	 */
	public static final String time2String(Timestamp timestamp){
		SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return sdfFormat.format(timestamp);
	}
	
	public static String toStr(Object obj) {
		return obj == null ? "" : obj.toString().trim();
	}

	/**
	 * 取当月最后一天
	 */
	public static String getLastDayOfMonth(Date sDate1) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(sDate1);
		calendar.set(Calendar.DATE, 1);
		calendar.roll(Calendar.DATE, -1);
		SimpleDateFormat simpleFormate = new SimpleDateFormat("yyyy-MM-dd");
		return simpleFormate.format(calendar.getTime());
	}

	/**
	 * 取当月第一天
	 */
	public static String getFirstDayOfMonth(Date sDate1) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(sDate1);
		calendar.set(Calendar.DATE, 1);
		SimpleDateFormat simpleFormate = new SimpleDateFormat("yyyy-MM-dd");
		return simpleFormate.format(calendar.getTime());
	}
	
	/**
	 * 获取上一年度
	 */
	public static String getBeforeYear(Date sDate1) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		return String.valueOf(Integer.parseInt(sdf.format(sDate1))-1);
	}
	
	/**
	 * 获取当前年度
	 */
	public static String getThisYear(Date sDate1) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		return sdf.format(sDate1);
	}

	/**
	 * 获取两个日期之间的月差
	 * 
	 * @param startDate
	 * @param endDate
	 * @return int
	 */
	public static int getMonthBetweenTwoDate(Date startDate, Date endDate) {
		int result = 0;
		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		start.setTime(startDate);
		end.setTime(endDate);
		result = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12
				+ end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
		return result;
	}

	/**
	 * 根据日期获取纳税期间(按月申报)
	 * 
	 * @param date
	 * @return String
	 */
	public static String getNsqjByDate(Date date) {
		SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
		SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
		//纳税期间做调整，例：税款所属期  2012-01-01 到 2012-01-31 其纳税期间为201202
		if("12".equals(sdfMonth.format(date))){
			return (Integer.parseInt(sdfYear.format(date))+1) + "01";
		}else{
			int month = Integer.parseInt(sdfMonth.format(date))+1;
			if(String.valueOf(month).length()==1){
				return sdfYear.format(date)+"0"+String.valueOf(month);
			}else{
				return sdfYear.format(date) + String.valueOf(month);	
			}
		}
	}

	/**
	 * 根据传入所属起止日期 计算上一期时间
	 * @param ssqq Calendar
	 * @param ssqz Calendar
	 * @return
	 */
	public static List<String> getSq(Calendar ssqq, Calendar ssqz) {

		Calendar lsSsqq = cloneCalendar(ssqq);
		Calendar lsSsqz = cloneCalendar(ssqz);

		List<String> al = new ArrayList<String>();
        int ssqqMonth = ssqq.get(Calendar.MONTH);
        int ssqzMonth = ssqz.get(Calendar.MONTH);
        int i = ssqzMonth - ssqqMonth + 1;
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	


            lsSsqq.add(Calendar.MONTH, - i);
            lsSsqq.set(Calendar.DAY_OF_MONTH, 1);

            lsSsqz.add(Calendar.MONTH, - i);
            lsSsqz.set(Calendar.DAY_OF_MONTH, lsSsqz
                       .getActualMaximum(Calendar.DATE));
            al.add(sdf.format(lsSsqq.getTime()));
            al.add(sdf.format(lsSsqz.getTime()));
        
        return al;
    }
	
	/**
	 * 根据传入所属起止日期 计算上一期时间
	 * @param ssqq String
	 * @param ssqz String
	 * @return
	 */
	public static List<String> getSq(String _ssqq, String _ssqz) {
		Calendar ssqq = Calendar.getInstance();
		ssqq.setTime(toDate(_ssqq));
		Calendar ssqz = Calendar.getInstance();
		ssqz.setTime(toDate(_ssqz));
		return getSq(ssqq,ssqz);
    }
	/**
	 * 根据传入所属起止日期 计算下一期时间
	 * @param _ssqq
	 * @param _ssqz
	 * @return
	 */
	public static List<String> getXq(String _ssqq, String _ssqz){
		Calendar ssqq = Calendar.getInstance();
		ssqq.setTime(toDate(_ssqq));
		Calendar ssqz = Calendar.getInstance();
		ssqz.setTime(toDate(_ssqz));
		return getXq(ssqq,ssqz);
	}
	/**
	 * 根据传入所属起止日期 计算上一期时间
	 * @param ssqq Calendar
	 * @param ssqz Calendar
	 * @return
	 */
	public static List<String> getXq(Calendar ssqq, Calendar ssqz) {

		Calendar lsSsqq = cloneCalendar(ssqq);
		Calendar lsSsqz = cloneCalendar(ssqz);

		List<String> al = new ArrayList<String>();
		int ssqqMonth = ssqq.get(Calendar.MONTH);
		int ssqzMonth = ssqz.get(Calendar.MONTH);
		int i = ssqzMonth - ssqqMonth + 1;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		lsSsqq.add(Calendar.MONTH,  i);
		lsSsqq.set(Calendar.DAY_OF_MONTH, 1);

		lsSsqz.add(Calendar.MONTH,  i);
		lsSsqz.set(Calendar.DAY_OF_MONTH,
				lsSsqz.getActualMaximum(Calendar.DATE));
		al.add(sdf.format(lsSsqq.getTime()));
		al.add(sdf.format(lsSsqz.getTime()));

		return al;
	}
	
	public static Calendar cloneCalendar(Calendar date){
		if(date==null){
			return null;
		}
		Calendar temp = Calendar.getInstance();
		temp.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH),date.get(Calendar.DAY_OF_MONTH),0,0,0);
		return temp;
	}
	
	
	/**
	 * 日期加减月份
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date dateMonthAdd(Date date,int amount){
		if(date==null){
			return null;
		}
		Calendar temp = Calendar.getInstance();
		temp.setTime(date);
		temp.add(Calendar.MONTH, amount);
		return temp.getTime();
	}
	
	/**
	 * 获取上个月的第一天
	 * @param date
	 * @return
	 */
	public static String getLastMonth(String date){
		String lastMonth="";
		String currentMM = String.valueOf((Integer.parseInt(date.substring(date.indexOf("-")+1,date.lastIndexOf("-"))) -1));
		if(currentMM.length() == 1){
			currentMM = "0"+currentMM;
		}
		if("00".equals(currentMM)){
			lastMonth = Integer.parseInt(date.substring(0, date.indexOf("-")))-1+"-12"+"-01";
		}else{
			lastMonth = date.substring(0, date.indexOf("-"))+"-"+currentMM+"-01";
		}
		return lastMonth;
	}
	
	public static String getLastDayOfMonth(String yyyymm) {
		  String year="";
		  String month="";
		  if(yyyymm.contains("-")){
			year= yyyymm.substring(0,4);
			month = yyyymm.substring(5);
		  }else{
			year=yyyymm.substring(0,4);
			month=yyyymm.substring(4);
		  }
		  Calendar cal = Calendar.getInstance();
		  //年
		  cal.set(Calendar.YEAR, Integer.parseInt(year));
		  //月，因为Calendar里的月是从0开始，所以要-1
		  cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		  //日，设为一号
		  cal.set(Calendar.DATE, 1);
		  //月份加一，得到下个月的一号
		  cal.add(Calendar.MONTH,1);
		  //下一个月减一为本月最后一天
		  cal.add(Calendar.DATE, -1);
		  return year+"-"+month+"-"+String.valueOf(cal.get(Calendar.DAY_OF_MONTH));//获得月末是几号
		 }
	
	/**
     * 获得当月的开始时间，即2014-01-01
     */
    public static String getMonthStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        String now = null;
        try {
            c.set(Calendar.DATE, 1);
            now = dateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前月的结束时间，即2014-01-31
     */
    public static String getMonthEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        String now = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            now = dateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前季度的开始时间，即1季度开始2014-01-01
     */
    public static String getQuarterStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        String now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = dateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前季度的开始时间，即1季度结束2014-03-31
     */
    public static String getQuarterEndTime(Date date) {
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        String now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = dateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获取前/后半年的开始时间
     */
    public static String getHalfYearStartTime(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        String now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6){
                c.set(Calendar.MONTH, 0);
            }else if (currentMonth >= 7 && currentMonth <= 12){
                c.set(Calendar.MONTH, 6);
            }
            c.set(Calendar.DATE, 1);
            now = dateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获取前/后半年的结束时间
     */
    public static String getHalfYearEndTime(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        String now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6){
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            }else if (currentMonth >= 7 && currentMonth <= 12){
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = dateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前年的开始时间，即2014-01-01
     */
    public static String getYearStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        String now = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            now = dateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前年的结束时间，即2014-12-31
     */
    public static String getYearEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        String now = null;
        try {
            c.set(Calendar.MONTH, 11);
            c.set(Calendar.DATE, 31);
            now = dateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    
	/**
	 * 根据传入所属起止日期 计算上一期时间
	 * @param ssqq
	 * @param ssqz
	 * @return
	 */
	public static Map<String,String> getSqqj(String _ssqq, String _ssqz) {
		Calendar ssqq = Calendar.getInstance();
		ssqq.setTime(toDate(_ssqq));
		Calendar ssqz = Calendar.getInstance();
		ssqz.setTime(toDate(_ssqz));
		
		Calendar lsSsqq = cloneCalendar(ssqq);
		Calendar lsSsqz = cloneCalendar(ssqz);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String,String> map = new HashMap<String,String>();
        int ssqqMonth = ssqq.get(Calendar.MONTH);
        int ssqzMonth = ssqz.get(Calendar.MONTH);
        int i = ssqzMonth - ssqqMonth + 1;

        lsSsqq.set(Calendar.MONTH, ssqqMonth - i);
        lsSsqq.set(Calendar.DAY_OF_MONTH, 1);
        lsSsqz.set(Calendar.MONTH, ssqqMonth - 1);
        lsSsqz.set(Calendar.DAY_OF_MONTH, lsSsqz
                   .getActualMaximum(Calendar.DATE));
        map.put("ssqq_ls",sdf.format(lsSsqq.getTime()));
        map.put("ssqz_ls",sdf.format(lsSsqz.getTime()));
        return map;
    }
	
	/**
	 * CA开通当期推算季度对应所属期
	 * @param caKtsj
	 * @return
	 */
	public static Map<String,String> sjdssqq(String caKtsj){
		int caKtMM = Integer.parseInt(caKtsj.substring(caKtsj.indexOf("-")+1,caKtsj.lastIndexOf("-")));//当前CA领取月份
		int currentSsnd = Integer.parseInt(caKtsj.substring(0,caKtsj.indexOf("-")));
		String currentSsqq = "";
		String currentSsqz = "";
		Map<String,String> map = new HashMap<String,String>();
		 switch (caKtMM) {  
           case 1:  
        	   currentSsqq = currentSsnd-1+"-10-01";
        	   currentSsqz = currentSsnd-1+"-12-31";
               break;  
           case 2:  
        	   currentSsqq = currentSsnd-1+"-10-01";
        	   currentSsqz = currentSsnd-1+"-12-31";
               break;  
           case 3:  
        	   currentSsqq = currentSsnd-1+"-10-01";
        	   currentSsqz = currentSsnd-1+"-12-31";
               break;  
           case 4:  
        	   currentSsqq = currentSsnd+"-01-01";
        	   currentSsqz = currentSsnd+"-03-31";
               break;  
           case 5:  
        	   currentSsqq = currentSsnd+"-01-01";
        	   currentSsqz = currentSsnd+"-03-31";
               break;  
           case 6:  
        	   currentSsqq = currentSsnd+"-01-01";
        	   currentSsqz = currentSsnd+"-03-31";
               break;  
           case 7:  
        	   currentSsqq = currentSsnd+"-04-01";
        	   currentSsqz = currentSsnd+"-06-30";
               break;  
           case 8:  
        	   currentSsqq = currentSsnd+"-04-01";
        	   currentSsqz = currentSsnd+"-06-30";
               break;  
           case 9:  
        	   currentSsqq = currentSsnd+"-04-01";
        	   currentSsqz = currentSsnd+"-06-30";
               break;  
           case 10:  
        	   currentSsqq = currentSsnd+"-07-01";
        	   currentSsqz = currentSsnd+"-09-30";
               break;  
           case 11:  
        	   currentSsqq = currentSsnd+"-07-01";
          	   currentSsqz = currentSsnd+"-09-30";
               break;  
           case 12:  
        	   currentSsqq = currentSsnd+"-07-01";
          	   currentSsqz = currentSsnd+"-09-30";
               break;  
           default:  
               break;  
       }  
		 map.put("currentSsqq", currentSsqq);
		 map.put("currentSsqz", currentSsqz);
		 return map;
	}
	
	public static String formatToStr(String  datestr,String format){
		Date date = DateUtil.toDate(datestr);
		String str = DateUtil.format(date,format);
		return str;
	}
	/**
	 * 根据某日与纳税期限获取所属期
	 * @param day nsqx
	 * @return
	 */
	public static Map<String,String> getSsqbyNsqx(String yyyymmdd,String nsqx){
		HashMap<String,String> map = new HashMap<String,String>();
		String date = formatToStr(yyyymmdd,"yyyy-MM-dd");
		String currentSsqq = "";
		String currentSsqz = "";
		if("20".equals(nsqx)){
			String yyyymm=date.substring(0,date.lastIndexOf("-"));
			currentSsqq = yyyymm+"-01";
			currentSsqz = getLastDayOfMonth(yyyymm);
		}else if("30".equals(nsqx)){
			int caKtMM = Integer.parseInt(date.substring(date.indexOf("-")+1,yyyymmdd.lastIndexOf("-")));//当前月份
			int currentSsnd = Integer.parseInt(date.substring(0,date.indexOf("-")));
			switch (caKtMM) {  
			case 1:  
				currentSsqq = currentSsnd+"-01-01";
				currentSsqz = currentSsnd+"-03-31";
				break;  
			case 2:  
				currentSsqq = currentSsnd+"-01-01";
				currentSsqz = currentSsnd+"-03-31";
				break;  
			case 3:  
				currentSsqq = currentSsnd+"-01-01";
				currentSsqz = currentSsnd+"-03-31";
				break;  
			case 4:  
				currentSsqq = currentSsnd+"-04-01";
				currentSsqz = currentSsnd+"-06-30";
				break;  
			case 5:  
				currentSsqq = currentSsnd+"-04-01";
				currentSsqz = currentSsnd+"-06-30";
				break;  
			case 6:  
				currentSsqq = currentSsnd+"-04-01";
				currentSsqz = currentSsnd+"-06-30";
				break;  
			case 7:  
				currentSsqq = currentSsnd+"-07-01";
				currentSsqz = currentSsnd+"-09-30";
				break;  
			case 8:  
				currentSsqq = currentSsnd+"-07-01";
				currentSsqz = currentSsnd+"-09-30";
				break;  
			case 9:  
				currentSsqq = currentSsnd+"-07-01";
				currentSsqz = currentSsnd+"-09-30";
				break;  
			case 10:  
				currentSsqq = currentSsnd+"-10-01";
				currentSsqz = currentSsnd+"-12-31";
				break;  
			case 11:  
				currentSsqq = currentSsnd+"-10-01";
				currentSsqz = currentSsnd+"-12-31";
				break;  
			case 12:  
				currentSsqq = currentSsnd+"-10-01";
				currentSsqz = currentSsnd+"-12-31";
				break;  
			default:  
				break;  
			}  
		}else if("40".equals(nsqx)){
			String yyyy=date.substring(0,4);
			currentSsqq = yyyy+"-01-01";
			currentSsqz = yyyy+"-12-31";
		}
		map.put("currentSsqq", currentSsqq);
		map.put("currentSsqz", currentSsqz);
		return map;
	}
	  /**
     * 定义常见的时间格式
     */
    private static String[] dateFormatall =
    {
        "yyyy-MM-dd HH:mm:ss",
        "yyyy/MM/dd HH:mm:ss",
        "yyyy年MM月dd日HH时mm分ss秒",
        "yyyy-MM-dd",
        "yyyy/MM/dd",
        "yy-MM-dd",
        "yy/MM/dd",
        "yyyy年MM月dd日",
        "HH:mm:ss",
        "yyyyMMddHHmmss",
        "yyyyMMdd",
        "yyyy.MM.dd",
        "yy.MM.dd"
    };
	
    public  static Calendar parseDate(String dateStr){
    	if(dateStr == null || dateStr.trim().length()==0 || "null".equals(dateStr.toLowerCase()))
            return null;

        Date result = parseDate(dateStr,0);
        Calendar cal = Calendar.getInstance();
        cal.setTime(result);

        return cal;
    }
    
    /**
     * 内部方法，根据某个索引中的日期格式解析日期
     * @param dateStr 期望解析的字符串
     * @param index 日期格式的索引
     * @return 返回解析结果
     */
    public  static Date parseDate(String dateStr,int index){
        DateFormat df = null;
        try{
            df = new SimpleDateFormat(dateFormatall[index]);

            return df.parse(dateStr);
        }catch(ParseException pe){
            return parseDate(dateStr,index+1);
        }catch(ArrayIndexOutOfBoundsException aioe){
             return null;
        }
    }
    
    public static int spanSsq(Calendar ssqq, Calendar ssqz) {
    	int iStart = ssqq.get(Calendar.MONTH);
    	int iEnd = ssqz.get(Calendar.MONTH);
    	return iEnd - iStart + 1;
        }
    /**
     * 将年月日时分秒转为年月日方式2014-12-04 00:00:00  ->2014-12-04
     * @param dateStr
     * @return
     */
    public static String formatDateStr(String dateStr) {
		return dateStr.substring(0, dateStr.indexOf(" "));
	}
    /**
     * 判断日期的有效性
     * @param year 年
     * @param month 月
     * @param day 日 
     * @return 返回检验结果
     */
    public static boolean isValidDate(String year, String month, String day ) {
		if(
				year == null || year.trim().equals("") || 
				month == null || month.trim().equals("") || 
				day == null || day.trim().equals("")
		){
			return false;
		}
		try{
			int d = Integer.parseInt(day);
			int m = Integer.parseInt(month);
			int y = Integer.parseInt(year);
			if(y < 1900 || y > 2200) return false;
		   
			if (d < 1 || m < 1 || m > 12) return false; 
		   
			if (m == 2) { 
				if (isLeapYear(y)) return d <= 29; 
				else return d <= 28; 
			} else if (m == 4 || m == 6 || m == 9 || m == 11) 
			  return d <= 30; 
			else  
			  return d <= 31; 
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	} 
	/*
	 * 判断是否为闰年  
	 * @param y 年 
	 * @return 返回结果  
	 */
    public static boolean isLeapYear(int y) {
		return y % 4 == 0 && (y % 400 == 0 || y % 100 != 0);  
	}
	
	public static void main(String[] args) throws ParseException {
//		Date date = DateUtil.dateFormat.parse("2014-07-02");
//		System.out.println(DateUtil.getMonthStartTime(date)+":"+DateUtil.getMonthEndTime(date));
//		System.out.println(DateUtil.getQuarterStartTime(date)+":"+DateUtil.getQuarterEndTime(date));
//		System.out.println(DateUtil.getHalfYearStartTime(date)+":"+DateUtil.getHalfYearEndTime(date));
//		System.out.println(DateUtil.getYearStartTime(date)+":"+DateUtil.getYearEndTime(date));
//		System.out.println(DateUtil.getSq(DateUtil.getMonthStartTime(date),DateUtil.getMonthEndTime(date)));
//		System.out.println(DateUtil.getSq(DateUtil.getQuarterStartTime(date),DateUtil.getQuarterEndTime(date)));
//		System.out.println(DateUtil.getSq(DateUtil.getHalfYearStartTime(date),DateUtil.getHalfYearEndTime(date)));
//		System.out.println(DateUtil.getSq(DateUtil.getYearStartTime(date),DateUtil.getYearEndTime(date)));
		System.out.println(DateUtil.getSsqbyNsqx("20141001", "20"));
	}
}
